/****************************************************
 * Description: Controllar for 用户组
 * Copyright:   Copyright (c) 2013
 * Company:     beiwaionline
 * @author      bfsu
 * @version     1.0
 * @see
	HISTORY
	*  2013-08-08 bfsu Create File
**************************************************/

package com.bfsuol.bfsuolframework.security.controller;


import com.bfsuol.bfsuolframework.core.controller.SpringControllerSupport;
import com.bfsuol.bfsuolframework.core.controller.json.MessageJson;
import com.bfsuol.bfsuolframework.core.controller.support.Pagination;
import com.bfsuol.bfsuolframework.core.exception.ValidationException;
import com.bfsuol.bfsuolframework.security.annotations.SecFunction;
import com.bfsuol.bfsuolframework.security.dao.BaseUser;
import com.bfsuol.bfsuolframework.security.dao.Group;
import com.bfsuol.bfsuolframework.security.service.BaseUserService;
import com.bfsuol.bfsuolframework.security.service.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/components/security/groupUser")
public class GroupUserController extends SpringControllerSupport {

    @Autowired
    private GroupService groupService;
    @Autowired
    private BaseUserService baseUserService;

    @SecFunction(title="组用户设置",privilege="components.security.group",code="userSetting")
    @RequestMapping("/index/{groupId}")
	public ModelAndView index(
			@PathVariable("groupId") Long groupId,
			Model model){
		Group group = groupService.getById(groupId);
		model.addAttribute("group", group);
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName(getViewNamedPath("index"));
		return modelAndView;
	}
	
	/**
	 * 根据用户组ID查询属于该用户组的所有用户（查询条件：名字模糊查询，登录名精确查询，Email模糊查询）
	 * @param groupId
	 * @param page
	 * @param model
	 * @return
	 */
    @SecFunction(title="组用户设置",privilege="components.security.group",code="userSetting")
	@RequestMapping("/list/{groupId}")
	public ModelAndView list(
			@PathVariable("groupId") Long groupId,
			@ModelAttribute("page") Pagination page,
			Model model){
		String name = getRequest().getParameter("name");
		String loginName = getRequest().getParameter("loginName");
		String email = getRequest().getParameter("email");
		page = baseUserService.findUserByGroup(name,loginName,email,groupId, page,true);
		model.addAttribute("name", name);
		model.addAttribute("loginName", loginName);
		model.addAttribute("email", email);
		model.addAttribute("groupId", groupId);

		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName(getViewNamedPath("list"));
		return modelAndView;
	}

    @SecFunction(title="组用户设置",privilege="components.security.group",code="userSetting")
	@RequestMapping("/{groupId}/delete/{userId}")
	public @ResponseBody MessageJson delete(
			@PathVariable("groupId") Long groupId,
			@PathVariable("userId") Long userId){
		Group group = groupService.getById(groupId);
		group.removeUser(new BaseUser(userId));
		groupService.updateById(group);
		return MessageJson.information("成功从该用户组删除1个用户");
	}

    @SecFunction(title="组用户设置",privilege="components.security.group",code="userSetting")
	@RequestMapping("/{groupId}/delete")
	public @ResponseBody MessageJson delete(
			@PathVariable("groupId") Long groupId,
			@RequestParam("ids") Long[] ids){
		if(ids == null || ids.length == 0){
			throw new ValidationException("没有删除");
		}
		groupService.deleteUsers(groupId,ids);
		return MessageJson.information("成功从该用户组删除"+ids.length+"个用户");
	}

    @SecFunction(title="组用户设置",privilege="components.security.group",code="userSetting")
	@RequestMapping("/addUsers/{groupId}")
	public @ResponseBody MessageJson addUsers(
			@PathVariable("groupId") Long groupId,
			@RequestParam("userIds") Long[] userIds){
		if(userIds != null && userIds.length > 0){
			Group group = groupService.getById(groupId);
			for(Long userId:userIds){
				group.addUser(new BaseUser(userId));
			}
			groupService.updateById(group);
			return MessageJson.success("成功添加"+userIds.length+"个用户");
		}else{
			throw new ValidationException("没有要添加的用户");
		}

	}
}
